<html>
<head>
    <style>
        h1,b{
        color: red;
        }
        .list{
            display: flex;
            border: 5px double red;
            padding: 10px;
            justify-content: space-around;
        }
        .list>div{
            border: 1px solid gray;
            padding: 8px;
            min-width: 64px;
            word-break: keep-all;

        }
        a{
            color: gray;
            text-decoration: none
        }
        a:hover{
            color: red;
            text-decoration: underline;
        }
    </style>
<body><?php
$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
$count=$db->query("select count(*) from chengyu")->fetchColumn();
$lanCount=11;
$chengyuCountPerLan=25;
$pageSize=$lanCount*$chengyuCountPerLan;
$pageTotal=ceil($count/$pageSize);
$page=(int)$_GET['page'];
if($page<1)$page=1;
else if($page>$pageTotal)$page=$pageTotal;
$n=6;
$begin=$page-$n;
$over=$page+$n;
if($begin<1){
    $begin=1;
    $over=2*$n+1;
}else if($over>$pageTotal){
    $over=$pageTotal;
    $begin=$pageTotal-2*$n;
} if($begin<1)$begin=1;
if($over>$pageTotal)$over=$pageTotal;
var_dump($page);
?>
<h1>中国成语大全</h1>
<div class="nav"><?php
for ($i=$begin;$i<=$over;$i++){
    if ($i==$page)echo "<b>$i</b>";
    else echo "<a href='?page=$i'>$i</a>";
}
    ?>
    <a href="?page=5">5</a>
    <a href="?page=6">6</a>
    <a href="?page=7">7</a>
</div>
<div class="list"><?php
 $ps=$db->prepare("select cy from chengyu limit ?,?");
 $ps->bindValue(1,($page-1)*$pageSize,PDO::PARAM_INT);
 $ps->bindValue(2,$pageSize,PDO::PARAM_INT);
 $ps->execute();
    for ($i=0; $i<=$lanCount;++$i){
        echo"<div>";
        for ($j=1;$j<=$chengyuCountPerLan;++$j){
            echo "<div>{$ps->fetchColumn()}</div>";
        }
        echo "</div>";
    }
    ?>
</body>
</head>
</html>

